package com.wiegand.gui.text {
	import com.wiegand.util.CropPic;
	import com.wiegand.loader.CssLoader;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.text.AntiAliasType;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	/**
	 * @author kw
	 */
	 
	public class CssTextBitmap extends Sprite{	
			
		static public function create( s : String, style : String, width : int = 0, exactFit : Boolean = false  ) : Bitmap {
					
			var textfield : TextField = new TextField();
			
			var textsprite : Sprite = new Sprite();
							
			if( width != 0 ){
				textfield.width = width;
				textfield.wordWrap = true;			
				textfield.multiline = true;				
			}				
										
			textfield.autoSize = TextFieldAutoSize.LEFT;	
			textfield.selectable = false;					
			textfield.mouseWheelEnabled = false;				
			textfield.embedFonts = true;								
			textfield.styleSheet = CssLoader.getInstance().getStyleSheet();
			textfield.htmlText = "<"+style+">"+s+"</"+style+">";	
			
			var css : Object = ( CssLoader.getInstance().getStyleSheet() as StyleSheet).getStyle( style );
							
			if( css["tf_antialiastype"] == "advanced" ) textfield.antiAliasType = AntiAliasType.ADVANCED;
			else textfield.antiAliasType = AntiAliasType.NORMAL;
						
			if( css["tf_sharpness"] != undefined ) textfield.sharpness = Number( css["tf_sharpness"] );			
			if( css["tf_thickness"] != undefined ) textfield.thickness = Number( css["tf_thickness"] );			
				
			textsprite.addChild( textfield );	
						
			var bitmapData : BitmapData = new BitmapData( textfield.width - 1, textfield.height, true );
			bitmapData.draw( textsprite, null, null, BlendMode.ALPHA, null, false);
			
			var bitmap : Bitmap = new Bitmap( bitmapData );
			
			textsprite.removeChild( textfield );
			textsprite = null;
			textfield = null;
			
			if( exactFit ){
			
				var data : BitmapData = CropPic.get_opaqueBMD( textfield ) as BitmapData;
				bitmap = new Bitmap( data );
			
			}
		
			return bitmap;
			
		}
	
	}
}
